﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace MU2DLIB.Graphic
{
    /// <summary>
    /// Public Sealed Class TextureManager
    /// Stores Texture2D Objects and offers Functions for Loading and Unloading.
    /// </summary>
    public sealed class TextureManager
    {
        #region Declaration

        /// <summary>
        /// Texture Buffer Size
        /// </summary>
        private const int TEXTUREBUFFER = 512;

        /// <summary>
        /// Texture Map
        /// </summary>
        private Dictionary<String, Texture2D> m_TextureMap = null;

        /// <summary>
        /// TextureManager Instance
        /// </summary>
        private static TextureManager m_Instance = null;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        private TextureManager()
        {
            this.m_TextureMap = new Dictionary<String, Texture2D>(TEXTUREBUFFER);
        }

        #endregion

        #region Properties

        public int TextureBuffer
        {
            get { return TEXTUREBUFFER; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Returns an Instance of the TextureManager Class
        /// </summary>
        /// <returns>Instance</returns>
        public static TextureManager GetInstance()
        {
            if (m_Instance == null)
                m_Instance = new TextureManager();

            return m_Instance;
        }

        /// <summary>
        /// Load a Texture into the Manager
        /// </summary>
        /// <param name="fileName">Texture File</param>
        /// <returns>Bool</returns>
        public bool LoadTexture(String fileName, ContentManager content) 
        {
            if (this.ContainsTexture(fileName))
                return false;

            try
            {
                this.m_TextureMap.Add(fileName, content.Load<Texture2D>(fileName));
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// loads a set of Textures into the Manager
        /// </summary>
        /// <param name="fileNames">String set</param>
        /// <returns>Bool</returns>
        public bool LoadTextureSet(String[] fileNames, ContentManager content)
        {
            if (fileNames == null)
                return false;

            try
            {
                foreach (String s in fileNames)
                {
                    if (!this.ContainsTexture(s))
                        this.m_TextureMap.Add(s, content.Load<Texture2D>(s));
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Unloads a single Texture from the Manager
        /// </summary>
        /// <param name="fileName">Texture File</param>
        /// <returns>Bool</returns>
        public bool UnloadTexture(String fileName)
        {
            return this.m_TextureMap.Remove(fileName);
        }

        /// <summary>
        /// Unloads a set of textures from the Manager
        /// </summary>
        /// <param name="fileNames">String set</param>
        /// <returns>Bool</returns>
        public bool UnloadTextureSet(String[] fileNames)
        {
            if (fileNames == null)
                return false;

            foreach (String s in fileNames)
            {
                this.m_TextureMap.Remove(s);
            }

            return true;
        }

        /// <summary>
        /// Returns a loaded Texture
        /// </summary>
        /// <param name="fileName">texture File</param>
        /// <returns>Texture2D</returns>
        public Texture2D GetTexture(String fileName)
        {
            Texture2D tmpTexture;

            if (this.m_TextureMap.TryGetValue(fileName, out tmpTexture))
                return tmpTexture;

            return null;
        }

        /// <summary>
        /// Returns a set of loaded Textures
        /// </summary>
        /// <param name="fileNames">String set</param>
        /// <returns>Texture2D set</returns>
        public Texture2D[] GetTextureSet(String[] fileNames)
        {
            if (fileNames == null)
                return null;

            Texture2D tmpTexture;
            List<Texture2D> textureSet = new List<Texture2D>(fileNames.Length);
            foreach (String s in fileNames)
            {
                if (this.m_TextureMap.TryGetValue(s, out tmpTexture))
                    textureSet.Add(tmpTexture);
            }

            return textureSet.ToArray();
        }

        /// <summary>
        /// Check if the Manager has loaded any Textures
        /// </summary>
        /// <returns>Bool</returns>
        public bool HasTextures()
        {
            if (this.m_TextureMap.Count == 0)
                return false;

            return true;
        }

        /// <summary>
        /// Check if a specific Texture is loaded
        /// </summary>
        /// <param name="fileName">Texture File</param>
        /// <returns>Bool</returns>
        public bool ContainsTexture(String fileName)
        {
            return this.m_TextureMap.ContainsKey(fileName);
        }

        /// <summary>
        /// Returns the number of loaded Textures
        /// </summary>
        /// <returns>Bool</returns>
        public int CountTexture()
        {
            return this.m_TextureMap.Count;
        }

        /// <summary>
        /// Unloads all Textures of the Manager
        /// </summary>
        public void ClearManager()
        {
            this.m_TextureMap.Clear();
        }

        #endregion
    }
}
